%ONOMA: GIANNOPOYLOS MENELAOS
%AEM:604
%EMAIL:megianno@uth.gr


clc;
clear;

% %FYSIKH KYVIKH PAREMVOLH
% %1. x=meres ths evdomadas  y=tse300 deikths z=nasdaq 
 x=[1,2,3,4,5];
 n=length(x)-1;
 h = x(2:n+1)-x(1:n);
 y=[7607.40 , 7544.60 , 7561.20 , 7526.40 , 7515.30];
 z=[1848.20, 1836.50, 1860.20, 1843.40, 1805.20];
 
 %2.
 A=[4, 1, 0;
     1, 4, 1;
     0,1,4];

%  b1=6/1^2*[y(1)-2*y(2)+y(3);
%      y(2)-2*y(3)+y(4);
%      y(3)- 2*y(4)+y(5)]
 
 f1 = 6*((y(3:n+1)-y(2:n))./h(2:n)-(y(2:n)-y(1:n-1))./h(1:n-1));
 f2 = 6*((z(3:n+1)-z(2:n))./h(2:n)-(z(2:n)-z(1:n-1))./h(1:n-1));
 
 %3.
m1 = A\f1';
m1 = [0;m1;0]';

m2 = A\f2';
m2= [0;m2;0]';

%  s=inv(A)*b;
% m(5)=0;
%  for k=2:1:4
%  m(k)=s(k-1);
%  end
%  m
 
%4.
 
 
 a1 = y(1:n);
 b1 = (y(2:n+1)-y(1:n))./h(1:n)-h(1:n).*(m1(2:n+1)+2*m1(1:n))/6;
 c1 = 0.5*m1(1:n);
 d1 = (m1(2:n+1)-m1(1:n))./(6*h(1:n));

 a2 = z(1:n);
 b2 = (z(2:n+1)-z(1:n))./h(1:n)-h(1:n).*(m2(2:n+1)+2*m2(1:n))/6;
 c2 = 0.5*m2(1:n);
 d2 = (m2(2:n+1)-m2(1:n))./(6*h(1:n));
 
     

%5,6

% % evaluation of cubic spline interpolants at data points xInt
xInt = 1 : 0.01: 5;       % another partition with better resolution
for j = 1 : length(xInt)
   if xInt(j)==2.5
       k=j;
   end
	if xInt(j) ~= x(n+1)
iInt(j) = sum(x <= xInt(j));
	else
		iInt(j) = n;
	end
end

yInt = a1(iInt) + b1(iInt).*(xInt-x(iInt)) + c1(iInt).*(xInt - x(iInt)).^2 + d1(iInt).*(xInt - x(iInt)).^3;
zInt = a2(iInt) + b2(iInt).*(xInt-x(iInt)) + c2(iInt).*(xInt - x(iInt)).^2 + d2(iInt).*(xInt - x(iInt)).^3;



plot(x,y,'b*',xInt,yInt,'g'); axis([1,5,7500,7700]);
xlabel ('hmeres ')
ylabel ('deikths')
title ('TSE300')

fprintf('Sta mesa ths tetarths: TSE= %5.2f   NASDAQ = %5.2f\n ',yInt(k),zInt(k));

pause
hold off

plot(x,z,'b*',xInt,zInt,'r'); axis([1,5,1800,1900]);
xlabel ('hmeres ')
ylabel ('deikths')
title ('NASDAQ')

pause
hold off


%kyvikh spline me paravolikh paremvolh sta akra
%2

A=[5, 1, 0;
     1, 4, 1;
     0,1,5];

 
 %3
 
 m1 = A\f1';
 

m1 = [m1(1);m1;m1(length(m1))]';

m2 = A\f2';
m2= [m2(1);m2;m2(length(m2))]';

%4 

a1 = y(1:n);
 b1 = (y(2:n+1)-y(1:n))./h(1:n)-h(1:n).*(m1(2:n+1)+2*m1(1:n))/6;
 c1 = 0.5*m1(1:n);
 d1 = (m1(2:n+1)-m1(1:n))./(6*h(1:n));

 a2 = z(1:n);
 b2 = (z(2:n+1)-z(1:n))./h(1:n)-h(1:n).*(m2(2:n+1)+2*m2(1:n))/6;
 c2 = 0.5*m2(1:n);
 d2 = (m2(2:n+1)-m2(1:n))./(6*h(1:n));

% %5,6

xInt = 1 : 0.01: 5;       % another partition with better resolution
for j = 1 : length(xInt)
   if xInt(j)==2.5
       k=j;
   end
	if xInt(j) ~= x(n+1)
iInt(j) = sum(x <= xInt(j));
	else
		iInt(j) = n;
	end
end

yInt = a1(iInt) + b1(iInt).*(xInt-x(iInt)) + c1(iInt).*(xInt - x(iInt)).^2 + d1(iInt).*(xInt - x(iInt)).^3;
zInt = a2(iInt) + b2(iInt).*(xInt-x(iInt)) + c2(iInt).*(xInt - x(iInt)).^2 + d2(iInt).*(xInt - x(iInt)).^3;



plot(x,y,'b*',xInt,yInt,'g'); axis([1,5,7500,7700]);
xlabel ('hmeres ')
ylabel ('deikths')
title ('TSE300')

fprintf('Sta mesa ths tetarths: TSE= %5.2f   NASDAQ = %5.2f\n ',yInt(k),zInt(k));

pause
hold off

plot(x,z,'b*',xInt,zInt,'r'); axis([1,5,1800,1900]);
xlabel ('hmeres ')
ylabel ('deikths')
title ('NASDAQ')

pause
hold off



%kyvikh spline me eniaia sumperifora sta akra


%2
A=[6, 1, 0;
     1, 4, 1;
     0,1,6];


 %3
 m1 = A\f1';
 m1 = [2*m1(1)-m1(2);m1;2*m1(length(m1))-m1(length(m1)-1)]';

m2 = A\f2';
m2= [2*m2(1)-m2(2);m2;2*m2(length(m2))-m2(length(m2)-1)]';

%4

a1 = y(1:n);
 b1 = (y(2:n+1)-y(1:n))./h(1:n)-h(1:n).*(m1(2:n+1)+2*m1(1:n))/6;
 c1 = 0.5*m1(1:n);
 d1 = (m1(2:n+1)-m1(1:n))./(6*h(1:n));

 a2 = z(1:n);
 b2 = (z(2:n+1)-z(1:n))./h(1:n)-h(1:n).*(m2(2:n+1)+2*m2(1:n))/6;
 c2 = 0.5*m2(1:n);
 d2 = (m2(2:n+1)-m2(1:n))./(6*h(1:n));
 
 % %5,6

xInt = 1 : 0.01: 5;       % another partition with better resolution
for j = 1 : length(xInt)
   if xInt(j)==2.5
       k=j;
   end
	if xInt(j) ~= x(n+1)
iInt(j) = sum(x <= xInt(j));
	else
		iInt(j) = n;
	end
end

yInt = a1(iInt) + b1(iInt).*(xInt-x(iInt)) + c1(iInt).*(xInt - x(iInt)).^2 + d1(iInt).*(xInt - x(iInt)).^3;
zInt = a2(iInt) + b2(iInt).*(xInt-x(iInt)) + c2(iInt).*(xInt - x(iInt)).^2 + d2(iInt).*(xInt - x(iInt)).^3;



plot(x,y,'b*',xInt,yInt,'g'); axis([1,5,7500,7700]);
xlabel ('hmeres ')
ylabel ('deikths')
title ('TSE300')

fprintf('Sta mesa ths tetarths: TSE= %5.2f   NASDAQ = %5.2f\n ',yInt(k),zInt(k));



pause
hold off

plot(x,z,'b*',xInt,zInt,'r'); axis([1,5,1800,1900]);
xlabel ('hmeres ')
ylabel ('deikths')
title ('NASDAQ')


zz=spline(xInt,zInt,2.5);
yy=spline(xInt,yInt,2.5);

yy-yInt(k);  % =0 ara einai to idio

pause
hold off




